class_name Notify
extends Node
var createListeners := []
var modifyListeners := []
var destroyListeners := []


func add_create_listener(fn, args):
	createListeners.append({"func": fn, "args": args})


func add_modify_listener(fn, args):
	modifyListeners.append({"func": fn, "args": args})


func add_destroy_listener(fn, args):
	destroyListeners.append({"func": fn, "args": args})


func create_event(id: int):
	var e = {"id": id}
	for iter in createListeners:
		iter.func.call_func(e,iter.args)


func modify_event(id: int, field: String):
	var e = {"id": id, "field": field}
	for iter in modifyListeners:
		iter.func.call_func(e,iter.args)


func destroy_event(id: int):
	var e = {"id": id}
	for iter in destroyListeners:
		iter.func.call_func(e,iter.args)


func get_destination_signal(destination: String) -> String:
	var dest_signal: String = "EcsEventBus|%s" % destination
	if not has_user_signal(dest_signal):
		add_user_signal(dest_signal)
	return dest_signal


func subscribe(destination: String, system: Node, callback_name: String) -> void:
	var dest_signal: String = get_destination_signal(destination)
	if not is_connected(dest_signal, system, callback_name):
		# warning-ignore: return_value_discarded
		connect(dest_signal, system, callback_name)


# Emits a payload to a destination. Any subscribed components will receive the payload
func emit(destination: String, payload) -> void:
	if not payload is Array:
		payload = [payload]
	payload.insert(0, get_destination_signal(destination))
	callv("emit_signal", payload)
